﻿using Learun.Application.Organization;
using Learun.Util;
using System;
using System.Collections.Generic;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-20 10:33
    /// 描 述：工程数据属性历史表
    /// </summary>
    public class ec_enginedata_propertyhisBLL : ec_enginedata_propertyhisIBLL
    {
        private ec_enginedata_propertyhisService ec_enginedata_propertyhisService = new ec_enginedata_propertyhisService();

        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_enginedata_propertyhisEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                string ProjectId = queryParam["ProjectId"]?.ToString();
                string PropertyName = queryParam["PropertyName"]?.ToString();
                string EngineDataID = queryParam["EngineDataID"]?.ToString();
                string TagNumber = queryParam["TagNumber"]?.ToString();

                string tableName = TableName<ec_enginedata_propertyhisEntity>(ProjectId);
                string enginedataTableName = TableName<ec_enginedataEntity>(ProjectId);
                var query = Db.Queryable<ec_enginedata_propertyhisEntity>().AS(tableName)
                    .InnerJoin<ec_enginedataEntity>((a, b) => a.EngineDataID == b.EngineDataID && a.CaseID == b.CaseID)
                        .AS<ec_enginedataEntity>(enginedataTableName)
                    .LeftJoin<UserEntity>((a, b, c) => a.CreateUserID == c.F_UserId)
                    .WhereIF(!string.IsNullOrEmpty(PropertyName), a => a.PropertyName == PropertyName)
                    .WhereIF(!string.IsNullOrEmpty(EngineDataID), a => a.EngineDataID == EngineDataID)
                    .WhereIF(!string.IsNullOrEmpty(TagNumber), (a, b) => b.TagNumber == TagNumber)
                    .Select((a, b, c) => new ec_enginedata_propertyhisEntity()
                    {
                        EngineDataPropertyHisID = a.EngineDataPropertyHisID,
                        EngineDataID = a.EngineDataID,
                        CaseID = a.CaseID,
                        PropertyName = a.PropertyName,
                        PropertyValue = a.PropertyValue,
                        CreateUserID = a.CreateUserID,
                        CreateTime = a.CreateTime,
                        UpdateUserID = a.UpdateUserID,
                        UpdateTime = a.UpdateTime,
                        CreateUserName = c.F_RealName
                    }).OrderByDescending(a => a.CreateTime);
                List<ec_enginedata_propertyhisEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    list = query.ToPageList(pagination.page, pagination.rows);
                }
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_enginedata_propertyhisEntity GetEntity(string keyValue)
        {
            try
            {
                return ec_enginedata_propertyhisService.GetEntity(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
